package myJs

import myJs.myPkg.{BootstrapTableJQueryImplicits, Layer, LayerOptions}
import scalatags.Text.all._

import scala.scalajs.js
import org.scalajs.jquery.JQuery

import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.myPkg.jquery._
import Implicits._
import org.scalajs.dom.raw.ProgressEvent

import scala.math.BigDecimal.RoundingMode



/**
  * Created by yz on 2019/2/26
  */
@JSExportTopLevel("Utils")
object Utils {

  implicit class MyJson(val json: js.Dictionary[js.Any]) {

    def myGetInt(key: String) = json(key).toString.toInt

  }

  implicit class MyJsArray(val array: js.Array[js.Dictionary[js.Any]]) {
    val emptyDic: js.Dictionary[js.Any] = js.Dictionary()

    def myHead = array.headOption.getOrElse(emptyDic)

  }

  implicit class BootstrapTableJQuery(val jq: JQuery) extends AnyVal {

  }

  val g = js.Dynamic.global
  val layer = g.layer.asInstanceOf[Layer]

  val zhInfo="信息"
  val layerOptions = LayerOptions.title(zhInfo).closeBtn(0).skin("layui-layer-molv").btn(js.Array())

  val zhRunning="正在运行"
  val element = div(id:="content")(
    span(id:="info")(zhRunning),
    " ",
    img(src := "/assets/images/running2.gif", width := 30, height := 20,cls:="runningImage")
  ).render

  val dataToggle=attr("data-toggle")
  val dataContent=attr("data-content")
  val dataContainer=attr("data-container")
  val dataPlacement=attr("data-placement")
  val dataHtml=attr("data-html")
  val dataAnimation=attr("data-animation")
  val dataTrigger=attr("data-trigger")
  val dataField=attr("data-field")
  val dataSortable=attr("data-sortable")

  @JSExport("setColumns")
  def setColumns(value: String,tableId:String="") = {
    val element = $(s"input:checkbox[value='${value}']")
    if (element.is(":checked")) {
      $(s"#${tableId}").bootstrapTable("showColumn",value)
    } else {
      $(s"#${tableId}").bootstrapTable("hideColumn",value)
    }
  }

  def progressHandlingFunction = {
    (e: ProgressEvent) => {
      if (e.lengthComputable) {
        val percent = e.loaded / e.total * 100
        val newPercent = BigDecimal(percent).setScale(2, RoundingMode.HALF_UP)
        $("#progress").html(s"(${newPercent}%)")
        if (percent >= 100) {
          $("#info").text("正在运行")
          $("#progress").html("")
        }
      }
    }

  }




}
